package com.cl.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cl.safe.R;
import com.cl.ui.FocusedTextView;
import com.cl.utils.MD5Utils;

public class HomeActivity extends Activity {
	private static String[] names = {
	         "手机防盗","通讯卫士","软件管理",
	         "进程管理","流量查询","手机杀毒",
	         "缓存清理","高级工具","设置中心"
	};
	private static int[] ids = {
		R.drawable.safe,R.drawable.callmsgsafe,R.drawable.app,
		R.drawable.taskmanager,R.drawable.netmanager,R.drawable.trojan,
		R.drawable.sysoptimize,R.drawable.atools,R.drawable.settings
	};
	private GridView gv;
	private FocusedTextView ftv;
	private SharedPreferences sp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		gv = (GridView) findViewById(R.id.gv_home_list);
		ftv = (FocusedTextView) findViewById(R.id.ftv_home);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		gv.setAdapter(new MyAdapter());
		gv.setOnItemClickListener(new OnItemClickListener() {
			Intent intent;
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				switch (position) {
				case 0://进入手机防盗界面
					showLostFinDialog();
					break;
				case 1://加载黑名单拦截界面
					intent = new Intent(HomeActivity.this,CallSmsSafeActivity.class);
					startActivity(intent);
					break;
				case 2://软件管理 
					intent = new Intent(HomeActivity.this,AppManagerActivity.class);
					startActivity(intent);
					break;
				case 3://进程管理
					intent = new Intent(HomeActivity.this,TaskManagerActivity.class);
					startActivity(intent);
					break;
				case 5://手机杀毒
					intent = new Intent(HomeActivity.this,AntiVirusActivity.class);
					startActivity(intent);
					break;
				case 6://手机杀毒
					intent = new Intent(HomeActivity.this,CleanCacheActivity.class);
					startActivity(intent);
					break;
				case 7:
					intent = new Intent(HomeActivity.this,AToolsActivity.class);
					startActivity(intent);
					break;
				case 8:
					intent = new Intent(HomeActivity.this,SettingActivity.class);
					startActivity(intent);
					break;
				
				default:
					break;
				}
				
			}

			private void showLostFinDialog() {
				if(isSetupPwd()) {
					//设置了密码，显示输入密码dialog
					showEnterPwdDialog();
				}else {
					//没有输入密码，进入设置密码dialog
					showSetupPwdDialog();
				}
				
			}
			
			private EditText et_setup_pwd;
			private EditText et_confirm_pwd;
			private Button ok;
			private Button cancer;
			private AlertDialog dialog;
			/**
			 * 设置密码对话框
			 */
			private void showSetupPwdDialog() {
				// TODO Auto-generated method stub
				View view = View.inflate(HomeActivity.this, R.layout.dialog_setup_password, null);
				et_setup_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
				et_confirm_pwd = (EditText) view.findViewById(R.id.et_setup_confirm);
				ok = (Button) view.findViewById(R.id.ok);
				cancer = (Button) view.findViewById(R.id.cancel);
				ok.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						String password = et_setup_pwd.getText().toString().trim();
						String pwd_confirm = et_confirm_pwd.getText().toString().trim();
						if(TextUtils.isEmpty(password) || TextUtils.isEmpty(pwd_confirm)) {
							Toast.makeText(HomeActivity.this, "密码不为空", 0).show();
							return;
						}else if(!password.equals(pwd_confirm)) {
							//密码不一致
							Toast.makeText(HomeActivity.this, "密码不一致", 0).show();
							return;
						}else {
							//密码一致
							Editor editor = sp.edit();
							editor.putString("password", MD5Utils.md5Password(password));
							editor.commit();
							//密码设置成功，进入LostFindActivity界面
							finish();
							Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
							startActivity(intent);
						}
					}

					
				});
				cancer.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						
					}
				});
				AlertDialog.Builder builder = new Builder(HomeActivity.this);
				dialog = builder.create();
				dialog.setView(view, 0, 0, 0, 0);
				dialog.show();
				
			}
			
			/**
			 * 輸入密碼對話框
			 */
			private EditText et_enter_pwd;
			private void showEnterPwdDialog() {
				View view = View.inflate(HomeActivity.this, R.layout.dialog_confirm_password, null);
				et_enter_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
				ok = (Button) view.findViewById(R.id.ok);
				cancer = (Button) view.findViewById(R.id.cancel);
				ok.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						String password = et_enter_pwd.getText().toString().trim();
						String configedPwd = sp.getString("password", "");
						if(TextUtils.isEmpty(password)) {
							Toast.makeText(HomeActivity.this, "密码不为空", 0).show();
							return;
						}else if(MD5Utils.md5Password(password).equals(configedPwd)) {
							//密码正确，进入LostFindActivity界面
							dialog.dismiss();
							Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
							startActivity(intent);
						}else {
							//密码不正确
							Toast.makeText(HomeActivity.this, "密码不正确", 0).show();
						}
					}
				});
				cancer.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						
					}
				});
				AlertDialog.Builder builder = new Builder(HomeActivity.this);
				dialog = builder.create();
				dialog.setView(view, 0, 0, 0, 0);
				dialog.show();
			}
			
			/**
			 * 判断是否已经设置了密码
			 * @return
			 */
			private boolean isSetupPwd() {
				String password = sp.getString("password", "");
				return !TextUtils.isEmpty(password);
			}
		});
	}
	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return names.length;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(HomeActivity.this, R.layout.item_list4home, null);
			ImageView iv = (ImageView) view.findViewById(R.id.iv_item);
			TextView tv = (TextView) view.findViewById(R.id.tv_item);
			iv.setImageResource(ids[position]);
			tv.setText(names[position]);
			return view;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		
	}
}
